import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutterdemo/utils/date_util.dart';

typedef bool Callback();

class FunctionPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _FunctionPageState();
  }
}

class _FunctionPageState extends State<FunctionPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('函数式编程'),
      ),
      body: _body(),
    );
  }

  Widget _body() {
    return Column(
      children: [
        Wrap(
          spacing: 5.0,
          runSpacing: 5.0,
          children: [
            OutlineButton(
              onPressed: () {
                test(() => isNoble(3));
              },
              child: Text('函数式编程'),
            ),
            OutlineButton(
              child: Text('函数作为变量'),
              onPressed: () {
                say('变量？');
              },
            ),
            OutlineButton(
              child: Text('函数作为参数'),
              onPressed: () {
                execute(() => print('函数作为参数'));
              },
            ),
            OutlineButton(
              child: Text('函数作为参数'),
              onPressed: () {
                execute(() => print('函数作为参数'));
              },
            ),
            OutlineButton(
              child: Text('时间测试'),
              onPressed: () {
                print(DateUtil.secondsFormat(
                    3700 * 1000, [HH, ":", nn, ":", ss]));
                print(DateTime.parse('2020-12-20')
                    .isAfter(DateTime.parse('2020-12-19 00:00:00')));
                print(DateUtil.dateStrFormat(
                    '2020-12-19 00:00:00', [yyyy, "-", mm, "-", dd]));
              },
            ),
          ],
        ),
      ],
    );
  }

  bool isNoble(int number) => number > 5;

  test(Callback callback) {
    print(callback());
  }

  //函数作为变量
  var say = (String str) {
    print(str);
  };

  void execute(var callback) {
    callback();
  }
}
